package com.ruoyi.common.utils;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * 时间工具类
 * @author: jiuya
 * @date: 2023/2/11 23:43
 * @description:
 */
public class DateTimeUtils {

    public static final String YEAR = "yyyy";
    public static final String YEAR_MONTH = "yyyy-MM";
    public static final String YEAR_MONTH_DAY = "yyyy-MM-dd";
    public static final String YEAR_MONTH_DAY_HOUR = "yyyy-MM-dd HH";
    public static final String YEAR_MONTH_DAY_HOUR_MINUTE = "yyyy-MM-dd HH:mm";
    public static final String YEAR_MONTH_DAY_HOUR_MINUTE_SECOND = "yyyy-MM-dd HH:mm:ss";

    public static final String HOUR_MINUTE = "HH:mm";
    public static final String HOUR_MINUTE_SECOND = "HH:mm:ss";

    private static final int DAY_SECOND = 86400;
    private static final int HOUR_SECOND = 3600;
    private static final int MINUTE_SECOND = 60;

    /**
     * 时间转字符串（yyyy）
     * @param date
     * @return
     */
    public static String formatYear(Date date) {
        LocalDateTime localDateTime = date.toInstant().atOffset(ZoneOffset.of("+8")).toLocalDateTime();
        return localDateTime.format(DateTimeFormatter.ofPattern(YEAR));
    }

    /**
     * 时间转字符串（yyyy-MM）
     * @param date
     * @return
     */
    public static String formatMonth(Date date) {
        LocalDateTime localDateTime = date.toInstant().atOffset(ZoneOffset.of("+8")).toLocalDateTime();
        return localDateTime.format(DateTimeFormatter.ofPattern(YEAR_MONTH));
    }

    /**
     * 时间转字符串（yyyy-MM-dd）
     * @param date
     * @return
     */
    public static String formatDate(Date date) {
        LocalDateTime localDateTime = date.toInstant().atOffset(ZoneOffset.of("+8")).toLocalDateTime();
        return localDateTime.format(DateTimeFormatter.ofPattern(YEAR_MONTH_DAY));
    }

    /**
     * 时间转字符串（yyyy-MM-dd HH）
     * @param date
     * @return
     */
    public static String formatDateTimeH(Date date) {
        LocalDateTime localDateTime = date.toInstant().atOffset(ZoneOffset.of("+8")).toLocalDateTime();
        return localDateTime.format(DateTimeFormatter.ofPattern(YEAR_MONTH_DAY_HOUR));
    }

    /**
     * 时间转字符串（yyyy-MM-dd HH:mm）
     * @param date
     * @return
     */
    public static String formatDateTimeM(Date date) {
        LocalDateTime localDateTime = date.toInstant().atOffset(ZoneOffset.of("+8")).toLocalDateTime();
        return localDateTime.format(DateTimeFormatter.ofPattern(YEAR_MONTH_DAY_HOUR_MINUTE));
    }

    /**
     * 时间转字符串（yyyy-MM-dd HH:mm:ss）
     * @param date
     * @return
     */
    public static String formatDateTime(Date date) {
        LocalDateTime localDateTime = date.toInstant().atOffset(ZoneOffset.of("+8")).toLocalDateTime();
        return localDateTime.format(DateTimeFormatter.ofPattern(YEAR_MONTH_DAY_HOUR_MINUTE_SECOND));
    }

    /**
     * 字符串转日期
     * @param dateStr
     * @return yyyy-MM-dd HH:mm:ss
     */
    public static Date formatDateTime(String dateStr) {
        DateTimeFormatter df = DateTimeFormatter.ofPattern(YEAR_MONTH_DAY_HOUR_MINUTE_SECOND);
        LocalDateTime dateTime = LocalDateTime.parse(dateStr, df);
        return Date.from(dateTime.toInstant(ZoneOffset.of("+8")));
    }

    /**
     * 时间比较
     * @param target 目标时间
     * @param time 对比时间
     * @return 如果对比时间小于目标时间那就返回 -1，如果等于就返回 0，如果大于返回 1
     */
    public static int compare(Date target, Date time) {
        if (time.getTime() < target.getTime()) {
            return -1;
        } else if (time.getTime() == target.getTime()) {
            return 0;
        }
        return 1;
    }
}
